home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / bsrc_p2.arc / MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-30  |  13.6 KB  |  592 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software <no-Inc>                   */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          No-Cost<no-tm> Software.                       */
  7. /*        \ 1011 /                                                          */
  8. /*         ------            KopyRong (K) 1987.  ALL RIGHTS REVERSED.       */
  9. /*                                                                          */
  10. /*                                                                          */
  11. /*                This module was written by Vince Perriello                */
  12. /*                     with code from several authors                       */
  13. /*                                                                          */
  14. /*                                                                          */
  15. /*                Miscellaneous routines used by BinkleyTerm                */
  16. /*                                                                          */
  17. /*                                                                          */
  18. /*   This  software  package is being distributed WITH FULL SOURCE CODE     */
  19. /*   with the  following  conditions:    1)  If  anything awful happens     */
  20. /*   because  you  use    it   (or  don't  use  it),  you  accept  full     */
  21. /*   responsibility;  2) you  don't start making tons of voice calls to     */
  22. /*   the authors to complain or  make  suggestions  about enhancements,     */
  23. /*   useful or otherwise;  3) you  do not reuse this code in commercial     */
  24. /*   products without specific permission to do so  from  the  authors;     */
  25. /*   4) If you find any problems you send  fixes  to  the  authors  for     */
  26. /*   inclusion  in  updates;    5) You find some way  to  express  your     */
  27. /*   appreciation  for  this  method of distribution, either by writing     */
  28. /*   code or  application  notes,  or  just sending along a "Thank You"     */
  29. /*   message.                                                               */
  30. /*                                                                          */
  31. /*   There is  copyrighted  code  in  this product.  We either wrote it     */
  32. /*   ourselves or got  permission  to use it.  Please don't force us to     */
  33. /*   pay a lawyer --  have some respect for our motives and don't abuse     */
  34. /*   this "license".                                                        */
  35. /*                                                                          */
  36. /*                                                                          */
  37. /*--------------------------------------------------------------------------*/
  38.  
  39. #include "xfer.h"
  40. #include "com.h"
  41. #include <dos.h>
  42. #include <time.h>
  43. #include <ctype.h>
  44. #include <stdio.h>
  45. #include <varargs.h>
  46.  
  47. extern struct baud_str btypes[];        /* the baud numbers, strings */
  48. extern int baud,cur_baud;            /* how we keep the baud rate */
  49. extern FILE *status_log;
  50. extern int got_arcmail;
  51. extern int loglevel;
  52. extern int un_attended;
  53. extern int fullscreen;
  54.  
  55. int have_dv = 0;
  56. int have_ddos = 0;
  57. int lock_baud = 0;
  58.  
  59.  
  60. char e_input[255];
  61. #define NUM_STATS 6
  62. #define STAT_WINX 0
  63. #define STAT_WINY 6
  64. char stat_lines[NUM_STATS][65];
  65. int nstat_lines = 0;
  66. char *Loc_msg = e_input;
  67.  
  68. char  *_s_mdm7idunno = "I dunno";
  69. char  *_s_mdm7giveup = "Give up";
  70. char  *_s_mdm7toolong = "File name too long";
  71. char  *_s_mdm7chksum = "Checksum";
  72.  
  73. char *BUNDLE_msg = " Sending bundle";
  74. char *FIND_msg = "find";
  75. char *IDUNNO_msg = "???";
  76. char *FLAGGING_msg = " Flagging %s as sent";
  77. char *NOTSENT_msg = " File %s not sent";
  78. char *TRUNC_msg = " File %s truncated";
  79. char *MEMOVFL_msg = " Memory Overflow";
  80. char *CAN_msg = " Transfer cancelled";
  81. char *KBD_msg = " Keyboard Escape";
  82. char *TIME_msg = " Timeout";
  83. char *CARRIER_msg = " Lost Carrier";
  84. char *READ_msg = " File Read";
  85. char *SEEK_msg = " File Seek";
  86. char *NAK_msg = "NAK";
  87. char *FUBAR_msg = " Too many errors";
  88. char *CMPL_msg = "Bad block complement";
  89. char *CRC_msg = "CRC";
  90. char *EOT_msg = "EOT";
  91. char *SYNC_msg = "Out of Sync";
  92. char *CHK_msg = "Checksum";
  93. char *SHRT_msg = "Short block";
  94. char *IO_msg = "I/O Error";
  95. char *CREATE_msg = "Create";
  96. char *CLOSE_msg = "Close";
  97. char *UNLINK_msg = "Unlink";
  98. char *OPEN_msg = "Open";
  99. char *WRITE_msg = "Write";
  100. char *NOTHING_msg = " Nothing transferred";
  101. char *local_CEOL = "\033[K";
  102.  
  103. mtask_find ()
  104. {
  105.    char buff[80];
  106.    unsigned int dv_get_version();
  107.  
  108.    scr_printf ("Multi-Tasker: ");
  109.    if ((have_dv = dv_get_version ()) != 0)
  110.       {
  111.       sprintf (buff, "DESQview version %d.%02d\r\n",
  112.          have_dv >> 8, have_dv & 0xff);
  113.       }
  114.    else if ((have_ddos = ddos_active()) != 0)
  115.       {
  116.       strcpy (buff, "DoubleDOS\r\n");
  117.       }
  118.    else
  119.       {
  120.       strcpy (buff, "None detected\r\n");
  121.       }
  122.  
  123.    scr_printf (buff);
  124. }
  125.  
  126. dexists(filename)
  127. char *filename;
  128. {
  129. struct FILEINFO dta;
  130. return (!dfind(&dta,filename,0));
  131. }
  132.  
  133. dfind (dta, name, times)
  134. struct FILEINFO *dta;
  135. char *name;
  136. int times;
  137. {
  138.     union REGS r;
  139.  
  140.     r.x.dx = (int)dta;
  141.     r.h.ah = 0x1a;
  142.     intdos (&r, &r);
  143.     r.x.bx = 0;
  144.     r.x.cx = ~0x08;
  145.     r.x.dx = (int)name;
  146.     r.x.si = 0;
  147.     r.x.di = 0;
  148.     if (times == 0)
  149.         {
  150.         r.h.ah = 0x4e;
  151.         intdos (&r, &r);
  152.         dta->nill = '\0';
  153.         if (r.x.cflag != 0)
  154.             {
  155.             dta->name[0] = '\0';
  156.             return (1);
  157.             }
  158.         return (0);
  159.         }
  160.     else
  161.         {
  162.         r.h.ah = 0x4f;
  163.         intdos (&r, &r);
  164.         dta->nill = '\0';
  165.         if (r.x.cflag != 0)
  166.             {
  167.             dta->name[0] = '\0';
  168.             return (1);
  169.             }
  170.         return (0);
  171.         }
  172.     }
  173.  
  174.  
  175. setmem (string,num,val)
  176. char *string;
  177. int num;
  178. char val;
  179. {
  180. register int i;
  181. for (i = 0; i < num; i++)
  182.     *string++ = val;
  183. }
  184.  
  185. set_baud(baudrate,log)
  186. unsigned baudrate;
  187. int log;
  188. {
  189. register int i;
  190.  
  191. if (!baudrate)                    /* "FAST" might do this      */
  192.     return(0);
  193.  
  194. if (ctl.max_baud && (baudrate > ctl.max_baud))
  195.     baudrate = ctl.max_baud;
  196. else if (lock_baud)
  197.    baudrate = ctl.max_baud;
  198.  
  199. for (i = 0; btypes[i].str != NULL; i++)
  200.     {
  201.     if (atoi(btypes[i].str) == baudrate)
  202.         {
  203.         if (baud != i)            /* same as what we have?     */
  204.             {
  205.             if (log && !un_attended) 
  206.                 status_line("#Setting baud to %d",baudrate);
  207.             baud = i;        /* need this for ALT-B       */
  208.             MDM_ENABLE(btypes[baud].rate);
  209.             cur_baud = baudrate;
  210.             }
  211.       if (un_attended && fullscreen)
  212.          {
  213.          gotoxy (73, 11);
  214.          cprintf ("%-5d", baudrate);
  215.          }
  216.         return(1);
  217.         }
  218.     }
  219. return(0);
  220. }
  221.  
  222. static char *specifiers = "!*+:# ";
  223. static char *mon[12] = {
  224.     "Jan","Feb","Mar","Apr","May","Jun",
  225.     "Jul","Aug","Sep","Oct","Nov","Dec"
  226.     };
  227. static struct tm *tp;
  228. static time_t ltime;
  229.  
  230. status_line(va_alist)
  231. va_dcl
  232. {
  233. char *fmt;
  234. va_list arg_ptr;
  235. int i;
  236. extern int errno;
  237. extern char *strchr();
  238.  
  239. va_start(arg_ptr);
  240. fmt = va_arg(arg_ptr, char *);
  241. errno = 0;
  242. vsprintf(e_input,fmt,arg_ptr);
  243. time(<ime);
  244. tp = localtime(<ime);
  245. if ((!fullscreen) || (!un_attended))
  246. {
  247. cprintf("\r\n%c %02i %03s %02i:%02i:%02i BINK %s",e_input[0],
  248.     tp->tm_mday,mon[tp->tm_mon],tp->tm_hour,tp->tm_min,tp->tm_sec,
  249.         &e_input[1]);
  250. }
  251. else
  252. {
  253. sprintf (stat_lines[nstat_lines], "%c %02i:%02i:%02i %-48.48s\r\n", e_input[0],
  254.    tp->tm_hour, tp->tm_min, tp->tm_sec, &e_input[1]);
  255. fill_in_status();
  256. if (++nstat_lines == NUM_STATS)
  257.    nstat_lines = 0;
  258. }
  259. if ((status_log != NULL) &&
  260.     ((strchr (specifiers, e_input[0]) - strchr(specifiers,'!')) <= loglevel))
  261.    {   
  262.    fprintf (status_log, "%c %02i %03s %02i:%02i:%02i BINK %s\n",e_input[0],
  263.     tp->tm_mday,mon[tp->tm_mon],tp->tm_hour,tp->tm_min,tp->tm_sec,
  264.         &e_input[1]);
  265.    fflush (status_log);
  266.    real_flush (fileno(status_log));
  267.    }
  268.  
  269. /*locate_x = wherex();
  270. locate_y = wherey();*/
  271. va_end(arg_ptr);
  272. }
  273.  
  274. /*--------------------------------------------------------------------------*/
  275. /* THROUGHPUT                                                               */
  276. /* Print throughput message at end of transfer                              */
  277. /*--------------------------------------------------------------------------*/
  278. void pascal throughput(opt, bytes)
  279.    int            opt;
  280.    unsigned long  bytes;
  281.    begin
  282.       static byte         *scrn     = "+CPS: %lu (%lu bytes)  Efficiency: %lu%%";
  283.       static unsigned long started  = 0L;
  284.       static unsigned long elapsed;
  285.       static unsigned long cps;
  286.  
  287.       if (!opt)   started  = time(NULL);
  288.       else if (started)
  289.          begin
  290.             elapsed  = time(NULL) - started;
  291.             if ((bytes < 1024L) || (elapsed == 0L)) return;
  292.             cps      = bytes / elapsed;
  293.             started  = (cps * 1000L) / ((long )cur_baud);
  294.             status_line(scrn,cps,bytes, started);
  295.          end
  296.  
  297.    end /* throughput */
  298.  
  299.  
  300.  
  301. fill_in_status()
  302. {
  303.    int i;
  304.  
  305. time(<ime);
  306. tp = localtime(<ime);
  307. gotoxy (STAT_WINX, STAT_WINY);
  308. for (i = nstat_lines+1; i < NUM_STATS; i++)
  309. {
  310.    cputs (stat_lines[i]);
  311. }
  312. for (i = 0; i <= nstat_lines; i++)
  313. {
  314.    cputs (stat_lines[i]);
  315. }
  316. gotoxy (69,8);
  317. cprintf ("%02i:%02i:%02i", tp->tm_hour, tp->tm_min, tp->tm_sec);
  318. gotoxy (69,9);
  319. cprintf ("%02i %03s %02i", tp->tm_mday, mon[tp->tm_mon], tp->tm_year);
  320. }
  321.  
  322. got_error(string1,string2)
  323. char *string1,*string2;
  324. {
  325. extern int errno;
  326. if (errno == 0x18)
  327.     errno = 0;
  328. if (errno != 0)
  329.     {
  330.     scr_printf("Can't ");
  331.     scr_printf(string1);
  332.     WRITE_ANSI(' ');
  333.     scr_printf(string2);
  334.     WRITE_ANSI('\n');
  335.     errno = 0;
  336.     return(1);
  337.     }
  338. return(0);
  339. }
  340.  
  341. set_xy(string)
  342. char *string;
  343. {
  344. WRITE_ANSI('\r');
  345. WRITE_ANSI('\n');
  346. if (*string != '\0')
  347.     {
  348.     scr_printf(string);
  349.     }
  350. locate_x = wherex();
  351. locate_y = wherey();
  352. }
  353.  
  354. message(string)
  355. char *string;
  356. {
  357. gotoxy(locate_x,locate_y);
  358. if (*string != '\0')
  359.     scr_printf(string);
  360. }
  361.  
  362. time_release()
  363. {
  364.    if (have_dv)
  365.       {
  366.       dv_pause();
  367.       }
  368.    else if (have_ddos)
  369.       {
  370.       ddos_pause();
  371.       }
  372. }
  373.  
  374. n_disable()
  375. {
  376. }
  377.  
  378. adios(n)
  379. int n;
  380. {
  381. MDM_DISABLE();
  382. exit(n);
  383. }
  384.  
  385. brk_disable()
  386. {
  387. }
  388.  
  389. clearbrk()
  390. {
  391. }
  392.  
  393. fancy_str(string)
  394. char *string;
  395. {
  396. register int flag = 0;
  397. while (*string)
  398.     {
  399.     if (isalpha(*string))                /* If alphabetic,     */
  400.         {
  401.         if (flag)                /* already saw one?   */
  402.             *string = tolower(*string);    /* Yes, lowercase it  */
  403.         else
  404.             {
  405.             flag = 1;            /* first one, flag it */
  406.             *string = toupper(*string);    /* Uppercase it       */
  407.             }
  408.         }
  409.     else                        /* if not alphabetic  */
  410.         flag = 0;                /* reset alpha flag   */
  411.     string++;
  412.     }
  413. }
  414.  
  415. timer(interval)
  416. int interval;
  417. {
  418. long timeout,timerset();
  419. timeout = timerset (interval * 10);
  420. while (!timeup(timeout))
  421.     time_release();
  422. }
  423.  
  424. big_pause(secs)
  425. int secs;
  426. {
  427. long timeout,timerset();
  428. timeout = timerset (secs * 100);
  429. while (!timeup(timeout))
  430.     {
  431.     if (CHAR_AVAIL())
  432.         exit;
  433.     time_release();
  434.     }
  435. }
  436.  
  437. unsigned int com_getc (t)
  438. int t;
  439. {
  440. unsigned char c;
  441. long t1;
  442. extern long timerset();
  443.  
  444. if (CHAR_AVAIL())
  445. {   
  446.    c = MODEM_IN();
  447. }
  448. else
  449. {
  450. t1 = timerset (t * 100);
  451. while (!CHAR_AVAIL())
  452.     {
  453.     if (timeup (t1))
  454.         {
  455.         return (EOF);
  456.         }
  457.  
  458. /* This should work because we only do TIMED_READ when we have carrier */
  459.     if (!CARRIER)
  460.         {
  461.         return (EOF);
  462.         }
  463.     time_release();
  464.     }
  465.    c = MODEM_IN();
  466. }
  467. return (c);
  468. }
  469.  
  470. /* Z F R E E -- Return total number of free bytes on drive specified */
  471.  
  472. long zfree(drive)
  473. char *drive;
  474. {
  475. union REGS r;
  476.  
  477. int driveno;
  478. long stat;
  479.  
  480. if (drive[0] != '\0' && drive[1]== ':' )
  481.     {
  482.     driveno = islower(*drive) ? toupper(*drive) : *drive;
  483.     driveno = driveno - 'A' + 1;
  484.     }
  485. else
  486.     driveno = 0;            /* Default drive    */
  487.  
  488. r.x.ax = 0x3600;            /* get free space    */
  489. r.h.dl = driveno;            /* on this drive    */
  490. int86(0x21,&r,&r);            /* go do it        */
  491.  
  492. if (r.x.ax == 0xffff)            /* error return??    */
  493.     return(0);
  494.  
  495. stat = (long)r.x.dx            /* dx = clusters avail    */
  496.      * (long)r.x.ax            /* ax = sectors/clust    */
  497.      * (long)r.x.cx;            /* cx = bytes/sector    */
  498.  
  499. return(stat);
  500. }
  501.  
  502. #ifdef NO_CRC_ASM
  503. /*
  504.  * This function calculates the CRC used by the XMODEM/CRC Protocol
  505.  * The first argument is the current CRC accumulator
  506.  * The second argument is the next byte to add into the CRC
  507.  * The function returns an integer which contains the CRC.
  508.  * The low order 16 bits are the coefficients of the CRC.
  509.  */
  510. unsigned int crc_update(crc, b)
  511. unsigned int crc;
  512. unsigned char b;
  513. {
  514.     register newcrc;
  515.     int i;
  516.  
  517.     newcrc = crc ^ (int)b << 8;
  518.     for (i = 0; i < 8; ++i)
  519.         if (newcrc & 0x8000)
  520.             newcrc = newcrc << 1 ^ 0x1021;
  521.         else
  522.             newcrc = newcrc << 1;
  523.     return (newcrc & 0xFFFF);
  524. }
  525. #endif
  526.  
  527. scr_printf(string)
  528. char *string;
  529. {
  530.     while (*string != 0)
  531.         WRITE_ANSI(*string++);
  532. }
  533.  
  534. void send_can()
  535.    begin
  536.        int i;
  537.       for(i=0; i<10; i++) SENDBYTE(CAN);
  538.       for(i=0; i<10; i++) SENDBYTE(BS);
  539.    end
  540.  
  541. invent_pkt_name(string)
  542. char string[];
  543. {
  544. struct tm *tp;
  545. time_t ltime;
  546. time(<ime);
  547. tp = localtime(<ime);
  548. sprintf(string,"%02i%02i%02i%02i.pkt",
  549.     tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);
  550. }
  551.  
  552. static char *suffixes[8] = {
  553.    "SU", "MO", "TU", "WE", "TH", "FR", "SA", NULL
  554. };
  555.  
  556. is_arcmail (p, n)
  557. char *p;
  558. int n;
  559. {
  560.    int i;
  561.    char c[128];
  562.  
  563.    if (!isdigit (p[n]))
  564.       {
  565.       return (0);
  566.       }
  567.  
  568.    strcpy (c, p);
  569.    strupr (c);
  570.  
  571.    for (i = n - 11; i < n - 3; i++)
  572.       {
  573.       if ((!isdigit(c[i])) && ((c[i] > 'F') || (c[i] < 'A')))
  574.          return(0);
  575.       }
  576.  
  577.    for (i = 0; i < 7; i++)
  578.       {
  579.       if (strnicmp (&c[n-2], suffixes[i], 2) == 0)
  580.          break;
  581.       }
  582.  
  583.    if (i >= 7)
  584.       {
  585.       return(0);
  586.       }
  587.  
  588.    got_arcmail = 1;
  589.    return (1);
  590. }
  591.  
  592.